﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Ajax.Common
{
    /// <summary>
    /// 取得服务端信息
    /// </summary>
    public class HttpServer
    {

        #region 测试服务器支持的对象
        /// <summary>
        /// 测试服务器支持的对象
        /// </summary>
        /// <param name="str">对象的名称</param>
        /// <returns>bool</returns>
        public bool CheckObject(string ObjectName)
        {
            try
            {
                object obj = HttpContext.Current.Server.CreateObject(ObjectName);
                return (true);
            }
            catch
            {
                return (false);
            }
        }
        #endregion

        /// <summary>
        /// 取得URL里面的网页名称
        /// </summary>
        /// <param name="URL"></param>
        /// <returns></returns>
        public static string GetFileName(string URL)
        {
            try
            {
                URL = URL.Replace(@"\", "/");
                int Index = URL.LastIndexOf('/') + 1;//移除前没有用的字符，只保留文件名和后缀
                return URL.Remove(0, Index);
            }
            catch
            {
                return URL;
            }
        }
         
        /// <summary>
        /// 获得绝对路径
        /// </summary>
        /// <param name="strPath">相对路径</param>
        /// <returns>绝对路径</returns>
        public static string MapPath(string strPath)
        {
            if (HttpContext.Current != null)
            {
                return HttpContext.Current.Server.MapPath(strPath);
            }
            else //非web程序引用
            {
                strPath = strPath.Replace("/", "\\");
                if (strPath.StartsWith("\\"))
                {
                    strPath = strPath.TrimStart('\\');
                }
                return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
            }
        }
        /// <summary>
        /// 前台获得绝对路径
        /// </summary>
        /// <param name="RootPath">根目录</param>
        /// <param name="RelativelyPath">相对路径</param>
        /// <returns>绝对路径</returns>
        public static string ForeMapPath(string RelativelyPath)
        {
            string RootDirectory = System.Configuration.ConfigurationManager.AppSettings["RootDirectory"];

            if (string.IsNullOrEmpty(RootDirectory))
            {
                return MapPath(RelativelyPath);
            }
            else
            {
                return System.IO.Path.Combine(RootDirectory, RelativelyPath);
            }
        }
 

        /// <summary>
        /// 判断当前页面是否接收到了Post请求
        /// </summary>
        /// <returns>是否接收到了Post请求</returns>
        public static bool IsPost
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("POST");
            }
        }
        /// <summary>
        /// 判断当前页面是否接收到了Get请求
        /// </summary>
        /// <returns>是否接收到了Get请求</returns>
        public static bool IsGet
        {
            get
            {
                return HttpContext.Current.Request.HttpMethod.Equals("GET");
            }
        }
    }
}